package fun.codefarmer.pm.controller.management.basic;

import fun.codefarmer.pm.model.DepartMent;
import fun.codefarmer.pm.model.RespBean;
import fun.codefarmer.pm.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ ClassName DepartmentController
 * @ Descriotion TODO
 * @ author  codeFarmer_z
 * @ 公众号：码农教程
 * @ Date 2022/4/13 19:53
 **/
@RequestMapping("/system/basic/department")
@RestController
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @GetMapping("/")
    public List<DepartMent> getAllDepartment() {
        List<DepartMent> list = departmentService.getAllDepartment();
        return list;
    }

    /**
     * 添加部门--利用存储过程
     * @param department
     * @return
     */
    @PostMapping("/")
    public RespBean addDep(@RequestBody DepartMent department) {
        departmentService.addDep(department);
        if (department.getResult() == 1) {
            return RespBean.ok("添加成功",department);
        }
        return RespBean.error("添加失败");
    }

    /**
     * 删除部门--存储过程--有员工或子部门，当前部门部能删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public RespBean deleteDep(@PathVariable Integer id) {
        RespBean resp = departmentService.deleteDep(id);
        return resp;
    }
}
